package eu.christophburmeister;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.UnknownHostException;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
 * Mojo-class that holds the execution for the goal "include-hostname". This
 * goal will get the hostname of the building machine and stores it in a
 * configurable file inside META-INF.
 * 
 * @author christoph
 * 
 */
@Mojo(name = "include-hostname", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, requiresDirectInvocation = false, requiresOnline = false, requiresProject = true, requiresReports = false, threadSafe = false)
public class HostnameMojo extends AbstractMojo {

	/** The File from project.build.directory-property */
	@Parameter(defaultValue = "${project.build.directory}")
	File projectBuildDirectory;

	/**
	 * The String that is configured inside the plugin-configuration. Default is
	 * set to "hostname.txt".
	 */
	@Parameter(alias = "filename", defaultValue = "hostname.txt", readonly = true, required = true)
	String hostnameFilename;

	/** The file-separator. */
	private static final String FILE_SEPARATOR = System.getProperty("file.separator");

	/** The relative path to meta-inf-directory under project-build-directory. */
	private static final String RELATIVE_META_INF_DIRECTORY = FILE_SEPARATOR + "classes" + FILE_SEPARATOR + "META-INF"
			+ FILE_SEPARATOR;

	/**
	 * The execute-method for the "include-hostname"-goal.
	 * 
	 * @throws MojoExecutionException
	 *             , MojoFailureException
	 */
	public void execute() throws MojoExecutionException, MojoFailureException {
		File metaInfDirectory = new File(projectBuildDirectory.getAbsolutePath() + RELATIVE_META_INF_DIRECTORY);

		getLog().info(">>> getting hostname");
		String hostname;
		try {
			hostname = getHostname();
		} catch (UnknownHostException e) {
			throw new MojoExecutionException(e.getMessage());
		}
		getLog().info(">>> found hostname: " + hostname);

		getLog().info(">>> creating directories to meta-inf");
		metaInfDirectory.mkdirs();
		getLog().info(">>> created directories to meta-inf: " + metaInfDirectory.getAbsolutePath());

		getLog().info(">>> writhing hostname to file");
		File hostnameFile = new File(metaInfDirectory, hostnameFilename);
		try {
			FileWriter fw = new FileWriter(hostnameFile);
			fw.write("built on " + hostname);
			getLog().info(">>> wrote hostname to file: " + hostnameFile.getAbsolutePath());
			fw.close();
		} catch (IOException e) {
			throw new MojoExecutionException(e.getMessage());
		} finally {
			//
		}
	}

	/**
	 * Private method to return the hostname of the underlying machine.
	 * 
	 * @return the hostname
	 * @throws UnknownHostException
	 */
	private String getHostname() throws UnknownHostException {
		return java.net.InetAddress.getLocalHost().getHostName();
	}
}
